package ysw.action;

import static com.opensymphony.xwork2.Action.NONE;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.FileInputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import ysw.jdbc.EmployeeJdbc;
import ysw.vo.Employee;

public class EmployeeAction extends ActionSupport{
	private Employee employee;
private File photofile;
  public File getPhotofile() {
        return photofile;
    }

    public void setPhotofile(File photofile) {
        this.photofile = photofile;
    }
    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
 
    private List<Employee> employeeList;
    public String execute() throws Exception{
        EmployeeJdbc employeeJ=new EmployeeJdbc();
             employee.setEid(employee.getEid());
            employee.setEname(employee.getEname());
           
            employee.setSalary(employee.getSalary());
            employee.setEjob(employee.getEjob());
             employee.setDname(employee.getDname());
              employee.setEphone(employee.getEphone());
            if(this.getPhotofile()!=null){
            FileInputStream fis=new FileInputStream(this.getPhotofile());
            byte[] buffer=new byte[fis.available()];
            fis.read(buffer);
            employee.setPhoto(buffer);
        }
        employeeJ.addEmployee(employee);
        return SUCCESS;
    }
    

public String showAllEmployee(){
     
	   Employee employee=new Employee();
    EmployeeJdbc employeeJ=new EmployeeJdbc();
        try{
            employeeList=employeeJ.showEmployee();
        }catch(SQLException e){
            e.printStackTrace();
        }
     
		Map request=(Map)ActionContext.getContext().get("request");
        request.put("employeeList",employeeList);
        return SUCCESS;
    }
public String deleteEmployee()throws Exception{
   //Employee employee=new Employee();
    EmployeeJdbc employeeJ=new EmployeeJdbc();
    employeeJ.deleteEmployee(employee.getEid());
    System.out.println(employee.getEid()+"employee.getEid()");
    return SUCCESS;
}
public String showOneEmployee() throws Exception{
     Employee employee1=new Employee();
    EmployeeJdbc employeeJ=new EmployeeJdbc();
    employee1=employeeJ.showOneEmployee(employee.getEid());
Map request=(Map)ActionContext.getContext().get("request");
request.put("employee1",employee1);
return SUCCESS;
}
public String getImage() throws Exception{
  HttpServletResponse response=ServletActionContext.getResponse();  
  Employee employee1=new Employee();
    EmployeeJdbc employeeJ=new EmployeeJdbc();
    Integer eid=employee.getEid();
    employee1=employeeJ.showOneEmployee(eid);
    byte[] img=employee1.getPhoto();
    response.setContentType("image/jpeg");
    ServletOutputStream os=response.getOutputStream();
    if(img!=null&&img.length!=0){
        for(int i=0;i<img.length;i++){
            os.write(img[i]);
        }
    }
    return NONE;
}
public String updateSaveEmployee() throws Exception{
       EmployeeJdbc employeeJ=new EmployeeJdbc();  
         employee.setEid(employee.getEid());
            employee.setEname(employee.getEname());
            employee.setSex(employee.getSex());
       
            employee.setSalary(employee.getSalary());
            employee.setEjob(employee.getEjob());
              employee.setDname(employee.getDname());
              employee.setEphone(employee.getEphone());
            if(this.getPhotofile()!=null){
            FileInputStream fis=new FileInputStream(this.getPhotofile());
            byte[] buffer=new byte[fis.available()];
            fis.read(buffer);
            employee.setPhoto(buffer);
        }
    employeeJ.updateSaveEmployee(employee);
    return SUCCESS;
}

}
